package online.sixfish.doubao.controller;

import com.vdurmont.emoji.EmojiParser;
import online.sixfish.common.api.ApiResult;
import online.sixfish.common.dto.PageDTO;
import online.sixfish.common.pojo.User;
import online.sixfish.doubao.model.dto.AddStudyGuideDTO;
import online.sixfish.doubao.model.entity.StudyGuide;
import online.sixfish.doubao.model.entity.StudyGuideComment;
import online.sixfish.doubao.model.vo.StudyGuideVO;
import online.sixfish.doubao.service.UserService;
import online.sixfish.doubao.service.StudyGuideService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/study-guide")
public class StudyGuideController {
    @Resource
    UserService userService;
    @Resource
    StudyGuideService studyGuideService;

    @GetMapping("/list")
    public ApiResult list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageDTO<StudyGuide> dto = studyGuideService.list(pageNum, pageSize);
        List<StudyGuideVO> list = dto.getData().stream().map(studyGuide -> {
            User user = userService.findById(studyGuide.getUserId());
            StudyGuideVO studyGuideVO = new StudyGuideVO();
            BeanUtils.copyProperties(studyGuide, studyGuideVO);
            studyGuideVO.setAlias(user.getAlias());
            studyGuideVO.setAvatar(user.getAvatar());
            studyGuideVO.setUsername(user.getUsername());
            return studyGuideVO;
        }).collect(Collectors.toList());
        PageDTO<StudyGuideVO> pageDTO = new PageDTO<>();
        pageDTO.setData(list);
        pageDTO.setTotal(dto.getTotal());
        pageDTO.setCurrentPageNum(dto.getCurrentPageNum());
        pageDTO.setCurrentPageCount(dto.getCurrentPageCount());
        return ApiResult.success(pageDTO);
    }

    @PostMapping("/add")
    @PreAuthorize("hasRole('user')")
    public ApiResult addArticle(@RequestBody AddStudyGuideDTO addStudyGuideDTO) {
        addStudyGuideDTO.setContent(EmojiParser.parseToAliases(addStudyGuideDTO.getContent()));
        String id = studyGuideService.add(addStudyGuideDTO);
        return ApiResult.success(id);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('user')")
    public ApiResult viewArticle(@PathVariable String id) {
        StudyGuide studyGuide = studyGuideService.getById(id);

        studyGuide.setContent(EmojiParser.parseToUnicode(studyGuide.getContent()));
//        eventPublisher.publishEvent(new ArticleViewEvent(this, studyGuide.getId()));
        return ApiResult.success(studyGuide);
    }

    @PostMapping("/comment/add")
    @PreAuthorize("hasRole('user')")
    public ApiResult addComment(@RequestBody StudyGuideComment comment) {
        studyGuideService.addComment(comment);
        return ApiResult.success();
    }

    @GetMapping("/comment/list")
    @PreAuthorize("hasRole('user')")
    public ApiResult listComment(@RequestParam String id,
                                 @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {
        PageDTO pageDTO = studyGuideService.listComment(id, pageNum, pageSize);
        return ApiResult.success(pageDTO);

    }

}
